<#include "/custom.include">
<#include "/java_copyright.include">
<#assign className = table.className>   
<#assign classNameLower = className?uncap_first>   
package ${basepackage}.app.controller;

import java.util.Hashtable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.mysql.jdbc.StringUtils;

import ${basepackage}.app.model.${className};
import ${basepackage}.app.service.${className}Service;
import ${basepackage}.app.vo.query.${className}Query;
import ${basepackage}.framework.base.controller.BaseSpringController;
import ${basepackage}.framework.base.page.Page;
import ${basepackage}.framework.base.web.Flash;
import ${basepackage}.framework.base.web.HttpUtils;
<#list table.importedKeys.associatedTables?values as foreignKey>
<#assign fkSqlTable = foreignKey.sqlTable>
<#assign fkTable    = fkSqlTable.className>
<#assign fkPojoClass = fkSqlTable.className>
<#assign fkPojoClassVar = fkPojoClass?uncap_first>
import ${basepackage}.app.service.${fkPojoClass}Service;
import ${basepackage}.app.model.${fkPojoClass};
</#list>

<#include "/author.include">

@Controller
@RequestMapping("${classNameLower}")
public class ${className}Controller extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	@Autowired
	private ${className}Service ${classNameLower}Service;
	
	<#list table.importedKeys.associatedTables?values as foreignKey>
	<#assign fkSqlTable = foreignKey.sqlTable>
	<#assign fkTable    = fkSqlTable.className>
	<#assign fkPojoClass = fkSqlTable.className>
	<#assign fkPojoClassVar = fkPojoClass?uncap_first>
	@Autowired
	private ${fkPojoClass}Service ${fkPojoClassVar}Service;
	
	private List<${fkPojoClass}> get${fkPojoClass}List(){
		return ${fkPojoClassVar}Service.findAll();
	}
	</#list>
	
	<#list table.columns as column>
	<#if column.isSecondSelectionColumn>
	@RequestMapping(value="/get${column.referenceTable}ListBy${column.referenceSelectionObject}")
	@ResponseBody
	public List<${column.referenceTable}> get${column.referenceTable}ListBy${column.referenceSelectionObject}(long id){
		return ${column.referenceTableLower}Service.findByProperty("${column.referenceSelectionObjectLower}",id);
	}
	
	
	</#if>
	</#list>
	
	private final String LIST_ACTION = "redirect:/${classNameLower}/list";
	
	public ${className}Controller() {
	}
	

	
	/** 
	 * 执行搜索 
	 **/
	@RequestMapping(value="/list")
	public ModelAndView list(HttpServletRequest request,${className}Query query) {
		String mode = request.getParameter("mode");
		if(!StringUtils.isNullOrEmpty(mode)){
			query = (${className}Query)request.getSession().getAttribute(${className}Query.class.getSimpleName());
		}
		
		Page page = this.${classNameLower}Service.findPage(query);
		request.getSession().setAttribute(${className}Query.class.getSimpleName(), query);
		ModelAndView result = new ModelAndView("/${className}/list");
		ModelMap map = toModelMap(page, query);
		<#list table.importedKeys.associatedTables?values as foreignKey>
		<#assign fkSqlTable = foreignKey.sqlTable>
		<#assign fkTable    = fkSqlTable.className>
		<#assign fkPojoClass = fkSqlTable.className>
		<#assign fkPojoClassVar = fkPojoClass?uncap_first>
		map.addAttribute("${fkPojoClassVar}List",get${fkPojoClass}List());
		</#list>	
		result.addAllObjects(map);
		return result;
	}
	
	/** 
	 * 查看对象
	 **/
	@RequestMapping(value="/show")
	public ModelAndView show(HttpServletRequest request) throws Exception {
		<@generateIdParameter/>
		${className} ${classNameLower} = (${className})${classNameLower}Service.getById(id);
		if(${classNameLower} == null){			
			Flash.current().error("error.record.notfound");
			return new ModelAndView("redirect:/${classNameLower}/list");
		}
		return new ModelAndView("/${className}/show","${classNameLower}",${classNameLower});
	}
	
	/** 
	 * 进入新增页面
	 **/
	@RequestMapping(value="/create")
	public ModelAndView create(${className} ${classNameLower}) throws Exception {
		ModelMap map = new ModelMap();
		map.addAttribute("${classNameLower}", ${classNameLower});
		<#list table.importedKeys.associatedTables?values as foreignKey>
		<#assign fkSqlTable = foreignKey.sqlTable>
		<#assign fkTable    = fkSqlTable.className>
		<#assign fkPojoClass = fkSqlTable.className>
		<#assign fkPojoClassVar = fkPojoClass?uncap_first>		
		map.addAttribute("${fkPojoClassVar}List",get${fkPojoClass}List());
		</#list>		
		
		
		return new ModelAndView("/${className}/create",map);
		
	}
	
	/** 
	 * 保存新增对象
	 **/
	@RequestMapping(value="/save")
	public ModelAndView save(@Valid ${className} ${classNameLower},BindingResult result) throws Exception {
		
		if(result.hasErrors()){
			//return new ModelAndView("/${className}/create","${classNameLower}",${classNameLower});
			return create(${classNameLower});
		}
		
		<#list table.importedKeys.associatedTables?values as foreignKey>
		<#assign fkSqlTable = foreignKey.sqlTable>
		<#assign fkTable    = fkSqlTable.className>
		<#assign fkPojoClass = fkSqlTable.className>
		<#assign fkPojoClassVar = fkPojoClass?uncap_first>		
		if(${classNameLower}.get${fkPojoClass}() != null && ${classNameLower}.get${fkPojoClass}().getId() == null){
			${classNameLower}.set${fkPojoClass}(null);
		}else if(${classNameLower}.get${fkPojoClass}().getId() != null){
			${classNameLower}.set${fkPojoClass}(${fkPojoClassVar}Service.getById(${classNameLower}.get${fkPojoClass}().getId()));
		}
		</#list>		
		
		${classNameLower}Service.save(${classNameLower});
		Flash.current().success(CREATED_SUCCESS); //存放在Flash中的数据,在下一次http请求中仍然可以读取数据,error()用于显示错误消息
		return new ModelAndView(LIST_ACTION);
	}
	
	private ModelAndView returnEditView(HttpServletRequest request,${className} ${classNameLower}){
		ModelMap map = new ModelMap();
		map.addAttribute("${classNameLower}", ${classNameLower});
		<#list table.importedKeys.associatedTables?values as foreignKey>
		<#assign fkSqlTable = foreignKey.sqlTable>
		<#assign fkTable    = fkSqlTable.className>
		<#assign fkPojoClass = fkSqlTable.className>
		<#assign fkPojoClassVar = fkPojoClass?uncap_first>
		map.addAttribute("${fkPojoClassVar}List",get${fkPojoClass}List());
		</#list>		
		return new ModelAndView("/${className}/edit",map);
	}
	
	/**
	 * 进入更新页面
	 **/
	@RequestMapping(value="/edit")
	public ModelAndView edit(HttpServletRequest request) throws Exception {
		<@generateIdParameter/>
		${className} ${classNameLower} = (${className})${classNameLower}Service.getById(id);
		
		if(${classNameLower} == null){			
			Flash.current().error("error.record.notfound");
			return new ModelAndView(LIST_ACTION);
		}
		
		return returnEditView(request,${classNameLower});
	}
	
	/**
	 * 保存更新对象
	 **/
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public ModelAndView update(HttpServletRequest request,@Valid ${className} ${classNameLower},BindingResult result) throws Exception {
		if(result.hasErrors()){
			returnEditView(request,${classNameLower});
		}
		
		<#list table.importedKeys.associatedTables?values as foreignKey>
		<#assign fkSqlTable = foreignKey.sqlTable>
		<#assign fkTable    = fkSqlTable.className>
		<#assign fkPojoClass = fkSqlTable.className>
		<#assign fkPojoClassVar = fkPojoClass?uncap_first>		
		if(${classNameLower}.get${fkPojoClass}() != null && ${classNameLower}.get${fkPojoClass}().getId() == null){
			${classNameLower}.set${fkPojoClass}(null);
		}else if(${classNameLower}.get${fkPojoClass}().getId() != null){
			${classNameLower}.set${fkPojoClass}(${fkPojoClassVar}Service.getById(${classNameLower}.get${fkPojoClass}().getId()));
		}
		</#list>
		
		${classNameLower}Service.update(${classNameLower});
		Flash.current().success(UPDATE_SUCCESS);
		return new ModelAndView(LIST_ACTION);
	}
	
	/**
	 *删除对象
	 **/
	@RequestMapping(value="/delete")
	public ModelAndView delete(HttpServletRequest request) {
		String[] items = request.getParameterValues("items");
		for(int i = 0; i < items.length; i++) {
			Hashtable params = HttpUtils.parseQueryString(items[i]);
			
			<#if table.compositeId>
			${className}Id id = (${className}Id)copyProperties(${className}Id.class,params);
			<#else>
				<#list table.compositeIdColumns as column>
			${column.javaType} id = new ${column.javaType}((String)params.get("${column.columnNameLower}"));
				</#list>
			</#if>
			
			${classNameLower}Service.removeById(id);
		}
		Flash.current().success(DELETE_SUCCESS);
		return new ModelAndView(LIST_ACTION);
	}
	
}

<#macro generateIdParameter>
	<#if table.compositeId>
		${className}Id id = new ${className}Id();
		bind(request, id);
	<#else>
		<#list table.compositeIdColumns as column>
		${column.javaType} id = new ${column.javaType}(request.getParameter("${column.columnNameLower}"));
		</#list>
	</#if>
</#macro>